<?php
namespace app\common\model;
class Device extends Base{
	/**
	 * 获取设备下的所有候选任务
	 * @param  integer 			$member_id 用户ID
	 * @param  integer||null 	$device_id 设备ID
	 * @param  array  $options   额外的参数
	 * @return array
	 */
	function getTaskQueue($member_id,$device_id=null,$options=[]){
		$options=array_merge([
			'is_busy'=>null,
			'state'=>null,
			'defaultSort'=>true,
		],$options);
		extract($options);

		$where=[
		    'd.member_id'=>$member_id,
		];
		if(!is_null($is_busy)){
			$where['d.is_busy']=$is_busy;
		}
		if(!is_null($state)){
			$where['td.state']=$state;
		}
		if(is_array($device_id)&&!empty($device_id)){
			$where['d.id']=['d.id','in',$device_id];
		}elseif(!is_null($device_id)&&$device_id>0){
			$where['d.id']=$device_id;
		}
		$list=$this->alias('d')
			->field('d.id as device_id,d.name as dev_name,td.state,td.sort,td.task_id,t.*,td.id,d.member_id')
			->leftJoin('task_device td','td.device_id=d.id')
			->leftJoin('tasking t','td.task_id=t.id')
			->where($where)
			->order('d.id,td.state desc,td.sort')
			->select();
		$list=$list->toArray();
		$rs=[];
		foreach ($list as $k => $v){
			if(isset($rs[$v['device_id']])){
				if(!empty($v['id'])){
					array_push($rs[$v['device_id']]['tasks'],$v);
				}
			}else{
				$temp=[
					'device_id' => $v['device_id'],
		           	'member_id' => $v['member_id'],
		           	'dev_name' => $v['dev_name'],
		           	'tasks'=>[]
				];
				if(!empty($v['id'])){
					$temp['tasks']=[$v];
				}
				$rs[$v['device_id']]=$temp;
			}
		}

		// 设备的排序 （下标重组）
		if($defaultSort){
			rsort($rs);
		}else{
			sort($rs);
		}
		
		return $rs;
	}
	
	/**
	 * 获取该会员下的所有设备ID
	 * @param  integer $member_id 用户ID
	 * @param  integer $status 状态
	 * @param  integer $leisure 当前是否在执行任务(仅在发布任务处该项不会失效)
	 * @return array
	 */
	function getDevice($member_id,$status=null,$leisure=false){
		$where=[
			'member_id'=>$member_id,
		];
		// 获取悠闲设备
		if($leisure){
			$where['is_busy']=0;
		}
		if(!is_null($status)){
			$where['status']=$status;
		}
		$list=$this->readAll($where,0,['field'=>'id']);
		$ids=[];
		foreach($list as $v) {
			array_push($ids,$v['id']);
		}
		return $ids;
	}

	// 用于Member/wsImData()
	function software(){
		return $this->hasMany('software','device_id')->field('id,device_id,number,name,id=0 as expand')->order('sort,id')->where(['status'=>1]);
	}

	// 通知后台设备空闲状态
	function isBusyNotice($uid,$dev_id,$is_busy){
		if(!$is_busy){ //空闲状态 进行用户微信消息收集
			$this->isCreateWxMsgTask($uid,$dev_id);
		}
		$data=[
			'event'=>'is_busy_notice',
			'data'=>[
				'is_busy'=>$is_busy,
				'dev_id'=>$dev_id,
			]
		];
		\socket\Socket::admin($data,$uid,$dev_id);
	}
	/**
	 * 是否创建微信消息采集任务
	 * @param  integer $uid    用户ID
	 * @param  integer $dev_id 设备ID
	 * @param  boolean $isCreate 是否强制创建任务(如果队列中不存在)
	 * @return void
	 */
	function isCreateWxMsgTask($uid,$dev_id,$isCreate=false){
		return; //禁用采集任务
		$time=time();
		$user=model('member')->read(['id'=>$uid],'id,wxmsg_interval,wxmsg_task_id');
		$dev=$this->read(['id'=>$dev_id],'id,wxmsg_stime,wxmsg_etime');
		if($dev['wxmsg_stime']==0||($dev['wxmsg_etime']>0&&$time-$user['wxmsg_interval']-$dev['wxmsg_etime']>0)||$isCreate){
			$rs=model('tasking')->pushTask($user['wxmsg_task_id'],$dev_id);
			if($rs['success']){
				$time=time();
				$this->edit(['id'=>$dev_id,'wxmsg_stime'=>$time,'wxmsg_etime'=>$time]);
			}
			return $rs;
		}
		return error();
	}

	/**
	 * 提供给前端筛选设备、微信号、好友等使用
	 * @param  integer $uid 用户ID
	 * @param  integer|array $ids 设备ID
	 * @return collection
	 */
	function tree($uid,$ids=0){
		$where=['member_id'=>$uid];
		$where['id']=$ids;
		if(is_array($ids)){
			$where['id']=['id','in',$ids];
		}
		return $this->readAll($where,0,[
			'field'=>'id,name',
			'with'=>['software','software.social']
		])->toJson();
	}
	
}
